package com.toolset.Activity.ToolItemActivity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.toolset.Activity.BaseActivity;
import com.toolset.R;

import org.apache.commons.jexl3.JexlBuilder;
import org.apache.commons.jexl3.JexlEngine;
import org.apache.commons.jexl3.JexlExpression;
import org.apache.commons.jexl3.ObjectContext;

public class Calculator extends BaseActivity {

    //按钮
    private Button num1;
    private Button num2;
    private Button num3;
    private Button num4;
    private Button num5;
    private Button num6;
    private Button num7;
    private Button num8;
    private Button num9;
    private Button ACBtn;
    private Button tuiBtn;
    private Button qiuYuBtn;
    private Button chuBtn;
    private Button chengBtn;
    private Button jianBtn;
    private Button num0;
    private Button dianBtn;
    private Button dengBtn;
    private Button jiaBtn;

    //输出框
    private EditText numDisplay;

    //记录输出框的文本
    private String displayContent = new String();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);

        //初始化事件
        initEvent();

        //在numDisplay加入文本 并且显示   // 添加数字
        addNumber(num1);
        addNumber(num2);
        addNumber(num3);
        addNumber(num4);
        addNumber(num5);
        addNumber(num6);
        addNumber(num7);
        addNumber(num8);
        addNumber(num9);
        addNumber(num0);

        //在numDisplay加入文本 并且显示   // 添加操作符号
        addOperator(jiaBtn);
        addOperator(jianBtn);
        addOperator(chengBtn);
        addOperator(chuBtn);
        addOperator(qiuYuBtn);
        addOperator(dianBtn);


        //设置清空按钮
        ACBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numDisplay.setText("");
                displayContent = "";

            }
        });

        //设置退一个字符按钮
        tuiBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                int length = numDisplay.length();
                //如果显示的内容不为空  减少一个字符
                if(length>0) {
                    displayContent = displayContent.substring(0, length - 1);
                    numDisplay.setText(displayContent.toString());
                }
                //如果显示的内容包含等于号  全部清空
                if(displayContent.indexOf("=")>0){
                    displayContent = "";
                    numDisplay.setText(displayContent.toString());
                }
            }
        });

        //设置等于操作
        dengBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //获取最后最后一个字符
                String lastChar = displayContent.substring(displayContent.length() - 1, displayContent.length());

                //如果有没等于号  与 最后一个字符是数字  执行等于操作
                if(displayContent.indexOf("=")<=0 && Character.isDigit(lastChar.charAt(0))) {

                    //创建JexlEngine
                    JexlEngine jexlEngine = new JexlBuilder().create();
                    //放入表达式
                    JexlExpression jexlExpression = jexlEngine.createExpression(displayContent);
                    //转换结果
                    Object value = jexlExpression.evaluate(null);
                    //跟前面字符串拼接
                    displayContent = displayContent + " = " + value.toString();
                    //显示
                    numDisplay.setText(displayContent.toString());
                }
            }
        });

    }




    //初始化控件
    private void initEvent(){
        num1 = (Button) findViewById(R.id.num1);
        num2 = (Button) findViewById(R.id.num2);
        num3 = (Button) findViewById(R.id.num3);
        num4 = (Button) findViewById(R.id.num4);
        num5 = (Button) findViewById(R.id.num5);
        num6 = (Button) findViewById(R.id.num6);
        num7 = (Button) findViewById(R.id.num7);
        num8 = (Button) findViewById(R.id.num8);
        num9 = (Button) findViewById(R.id.num9);
        num0 = (Button) findViewById(R.id.num0);
        ACBtn = (Button) findViewById(R.id.btnAC);
        tuiBtn = (Button) findViewById(R.id.btnTui);

        jiaBtn = (Button) findViewById(R.id.btnJia);
        jianBtn = (Button) findViewById(R.id.btnJian);
        chengBtn = (Button) findViewById(R.id.btnCheng);
        chuBtn = (Button) findViewById(R.id.btnChu);
        qiuYuBtn = (Button) findViewById(R.id.btnQiuYu);
        dianBtn = (Button) findViewById(R.id.btnDian);

        dengBtn = (Button) findViewById(R.id.btnDeng);

        numDisplay = (EditText) findViewById(R.id.num_display);
    }


    // 添加数字
    private void  addNumber(final Button button){
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = button.getText().toString();
                displayContent = displayContent +text;
                numDisplay.setText(displayContent.toString());
            }
        });
    }

    //添加+-*/等运算符
    private void addOperator(final Button button){
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //获取文本的符号
                String text = button.getText().toString();

                //判断输出框内容是否为空  不为空 执行添加运算符
                if(displayContent.length()>0) {


                    //获取最后字符
                    String lastChar = displayContent.substring(displayContent.length() - 1, displayContent.length());

                    //如果最后一个字符 不是数字
                    if(!Character.isDigit(lastChar.charAt(0))) {
                        //去除最后一个字符
                        displayContent = displayContent.substring(0, displayContent.length() - 1);
                    }
                    //添加点击按钮的字符
                    displayContent = displayContent + text;
                    //显示
                    numDisplay.setText(displayContent.toString());

                }

            }
        });
    }



}

